Add new public method `addFields()` to HTMLForm
authorDayllan Maza <dmaza@wikimedia.org>
Tue, 25 Jun 2019 16:02:33 +0000 (12:02 -0400)
committerDayllan Maza <dmaza@wikimedia.org>
Wed, 26 Jun 2019 20:29:41 +0000 (16:29 -0400)
Once you instantiate an HTMLForm there was no clear way of adding
new fields except for hidden fields. This is particularly problematic
when the form is passed by reference in Hooks.

NOTE: this is just moving what was previously part of the constructor
into its own method + very small tweaks

Change-Id: I23f983417510841ce76cdefcb076e5ab97b43f10

includes/htmlform/HTMLForm.php

index d071478..99e387a 100644 (file)
@@ -180,9 +180,8 @@ class HTMLForm extends ContextSource {
        protected $mMessagePrefix;
 
        /** @var HTMLFormField[] */
-       protected $mFlatFields;
-
-       protected $mFieldTree;
+       protected $mFlatFields = [];
+       protected $mFieldTree = [];
        protected $mShowReset = false;
        protected $mShowSubmit = true;
        protected $mSubmitFlags = [ 'primary', 'progressive' ];
@@ -315,7 +314,8 @@ class HTMLForm extends ContextSource {
        /**
         * Build a new HTMLForm from an array of field attributes
         *
-        * @param array $descriptor Array of Field constructs, as described above
+        * @param array $descriptor Array of Field constructs, as described
+        *      in the class documentation
         * @param IContextSource|null $context Available since 1.18, will become compulsory in 1.18.
         *     Obviates the need to call $form->setTitle()
         * @param string $messagePrefix A prefix to go in front of default messages
@@ -343,11 +343,23 @@ class HTMLForm extends ContextSource {
                        $this->displayFormat = 'div';
                }
 
-               // Expand out into a tree.
+               $this->addFields( $descriptor );
+       }
+
+       /**
+        * Add fields to the form
+        *
+        * @since 1.34
+        *
+        * @param array $descriptor Array of Field constructs, as described
+        *      in the class documentation
+        * @return HTMLForm
+        */
+       public function addFields( $descriptor ) {
                $loadedDescriptor = [];
-               $this->mFlatFields = [];
 
                foreach ( $descriptor as $fieldname => $info ) {
+
                        $section = $info['section'] ?? '';
 
                        if ( isset( $info['type'] ) && $info['type'] === 'file' ) {
@@ -371,7 +383,9 @@ class HTMLForm extends ContextSource {
                        $this->mFlatFields[$fieldname] = $field;
                }
 
-               $this->mFieldTree = $loadedDescriptor;
+               $this->mFieldTree = array_merge( $this->mFieldTree, $loadedDescriptor );
+
+               return $this;
        }
 
        /**
@@ -454,7 +468,8 @@ class HTMLForm extends ContextSource {
         * @since 1.23
         *
         * @param string $fieldname Name of the field
-        * @param array &$descriptor Input Descriptor, as described above
+        * @param array &$descriptor Input Descriptor, as described
+        *      in the class documentation
         *
         * @throws MWException
         * @return string Name of a HTMLFormField subclass
@@ -481,7 +496,8 @@ class HTMLForm extends ContextSource {
         * Initialise a new Object for the field
         *
         * @param string $fieldname Name of the field
-        * @param array $descriptor Input Descriptor, as described above
+        * @param array $descriptor Input Descriptor, as described
+        *      in the class documentation
         * @param HTMLForm|null $parent Parent instance of HTMLForm
         *
         * @throws MWException